#include <QtGui>
#include "yzjreader.h"

YzjReader::YzjReader(QTreeWidget *treeWidget)
    : treeWidget(treeWidget)
{

    treeWidget->clear();
    QStyle *style = treeWidget->style();

    folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirClosedIcon),
                         QIcon::Normal, QIcon::Off);
    folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirOpenIcon),
                         QIcon::Normal, QIcon::On);
    bookmarkIcon.addPixmap(style->standardPixmap(QStyle::SP_FileIcon));
}

bool YzjReader::ReadYzj(QIODevice *device)
{
    xml.setDevice(device);

    if (xml.readNextStartElement()) {
        if (xml.name() == "yzj" && xml.attributes().value("version") == "1.0")
            readYZJ();
        else
            xml.raiseError(QObject::tr("The file is not an YZJ version 1.0 file."));
    }

    return !xml.error();
}

QString YzjReader::errorString() const
{
    return QObject::tr("%1\nLine %2, column %3")
            .arg(xml.errorString())
            .arg(xml.lineNumber())
            .arg(xml.columnNumber());
}

void YzjReader::readYZJ()
{
    Q_ASSERT(xml.isStartElement() && xml.name() == "yzj");

    while (xml.readNextStartElement()) {
        if (xml.name() == "Category")
            readCategory(0);
        else if (xml.name() == "Item")
            readItem(0);
        else if (xml.name() == "Record")
            readRecord(0);
        else
            xml.skipCurrentElement();
    }
}

void YzjReader::readRecord(QTreeWidgetItem *item)
{
    Q_ASSERT(xml.isStartElement() && xml.name() == "Record");

//    QString records = xml.attributes().value("count");
}

void YzjReader::readTitle(QTreeWidgetItem *item)
{
    Q_ASSERT(xml.isStartElement() && xml.name() == "Title");

    QString title = xml.readElementText();
    item->setText(0, title);
}

void YzjReader::readKey(QTreeWidgetItem *item){
        Q_ASSERT(xml.isStartElement()&& xml.name() == "Key");
        QString key = xml.readElementText();
        item->setText(0,key);
}

void YzjReader::readCategory(QTreeWidgetItem *item)
{
    Q_ASSERT(xml.isStartElement() && xml.name() == "Category");

    QTreeWidgetItem *folder = createChildItem(item);
    bool folded = (xml.attributes().value("folded") != "no");
    treeWidget->setItemExpanded(folder, !folded);

    while (xml.readNextStartElement()) {
        if (xml.name() == "Title")
            readTitle(folder);
        else if (xml.name() == "Category")
            readCategory(folder);
        else if (xml.name() == "Item")
            readItem(folder);
        else
            xml.skipCurrentElement();
    }
}

// 同时负责将数据写入QList的问题
void YzjReader::readItem(QTreeWidgetItem *item)
{
    Q_ASSERT(xml.isStartElement() && xml.name() == "Item");

    QTreeWidgetItem *bookmark = createChildItem(item);
    bookmark->setFlags(bookmark->flags() | Qt::ItemIsEditable);
    bookmark->setIcon(0, bookmarkIcon);
    bookmark->setText(0, QObject::tr("Unknown title"));
    QString value = xml.attributes().value("Value").toString();
    // 插入单个数值
    lValue.push_back(value.toFloat());//.toFloat(true));

    bookmark->setText(1, value);
    lValue.push_back(value.toFloat());

    while (xml.readNextStartElement()) {
        if (xml.name() == "Key")
                readKey(bookmark);
        else
            xml.skipCurrentElement();
    }
}

QTreeWidgetItem *YzjReader::createChildItem(QTreeWidgetItem *item)
{
    QTreeWidgetItem *childItem;
    if (item) {
        childItem = new QTreeWidgetItem(item);
    } else {
        childItem = new QTreeWidgetItem(treeWidget);
    }
    childItem->setData(0, Qt::UserRole, xml.name().toString());
    return childItem;
}

// 获取从yzj文件中读取的数值，用于异常分析
/*
QList<float> YzjReader::GetValue()
{
    return lValue;
}
*/
